<?php
set_time_limit(0);
//ini_set("display_errors","2"); ERROR_REPORTING(E_ALL);
include_once (PATH_MODEL .'dUnzip2.inc.php');
include_once (PATH_MODEL .'dZip.inc.php');
include_once (PATH_MODEL .'delfolder.php');
include_once (PATH_SQL . 'template_sql.php');
include_once (PATH_MODEL . 'template_model.php');
include_once (PATH_VIEW_ADMIN . 'template_view.php');
$oModelTem = new Model_Template();
$oViewTem = new View_Admin_TemplateManangement($oModelTem);
$oModelTem->setData('page_size',50);
$oModelTem->setData('pagegroup_size',5);
$oModelTem->setData('stylesheet',"paging");
$sAction = $_REQUEST['sAction'];
$oUpload = new Upload($_FILES);
switch ($sAction) {
    case '': {
            $oViewTem->displayManagerIndex($oModelTem);
            $oViewTem->displayListTemplate($oModelTem);
            $oModelTem->unsetData('status');
            break;
        }
    case 'update_status': {
            $oModelTem->unsetData('product_category_id');
            $oModelTem->unsetData('product_category_status');
            if (count($_POST['hotel_page_sort'] > 0)) {
                foreach ($_POST['hotel_page_sort'] as $sKey => $sValue) {
                    $oModelTem->setData('product_category_id', $sKey);
                    $oModelTem->setData('product_category_status', intval($_POST['product_category_status'][$sKey]));
                    $oModelTem->updateCategory();
                }
            }
            redir('?sMod=category_index');

            break;
        }
    case 'add_new_form': {
            $oModelTem->readTemplate();
            $oViewTem->displayNewTemplateIndex($oModelTem);
            break;
        }
    case 'do_add_new': {
            $check_exits=$oModelTem->check_exit_template_id($_POST['id']);

            if($check_exits!=false) {
                redir('?sMod=template_index&status=exits_id');
            }
            if ($_POST['status'] != 'NULL')
                $oModelTem->setData('status', 1);
            else
                $oModelTem->setData('status', 0);

            if ($_POST['active_default'] != 'NULL')
                $oModelTem->setData('active_default', 1);
            else
                $oModelTem->setData('active_default', 0);

            if($_POST['id'] == '') {
                unset($_POST['id']);
            }else{
                $oModelTem->setData( 'id', $_POST['id'] );
            }

            $oModelTem->setData('explanation', $_POST['explanation']);
            $oModelTem->setData('damage', $_POST['damage']);
            $oModelTem->setData('dagame', $_POST['dagame']);
            $oModelTem->setData('notes', $_POST['notes']);
            $oModelTem->setData('size', $_POST['size']);
            
            $check_insert = $oModelTem->insertTemplate();
            $oModelTem->setData('id', $check_insert);
            $oModelTem->updateTemplateFiles();
            $oModelTem->setData('path', "/tem".$value['id']);
            $oModelTem->updateTemplate();
            redir('?sMod=template_index&status=addSuccess');
            break;
        }
    case 'check_exits_id': {
            $id = $_REQUEST['checkid'];
            $oViewTem->resultXML($oModelTem, '', $oModelTem->check_exit_category_id($id));
            die();
            break;
        }
    case 'do_delete': {
            $id = $_GET['id'];
            $oModelTem->setData('id', $id);
            $row = $oModelTem->getInfoTemplate($id);
            $result = $oModelTem->deleteTemplate($id);
            $oModelTem->removedir();
            if(!$result) {
                redir('?sMod=template_index&status=delError');
            }else {

                redir('?sMod=template_index&status=delSuccess');
            }
            break;
        }
    case 'edit_form': {
            
            $id = $_GET['id'];
            $oModelTem->setData('id', $id);
            $oModelTem->readTemplate();
            $oViewTem->displayEditTemplateIndex($oModelTem);
            $rowManager = $oModelTem->getInfoTemplate($id);
            $oViewTem->displayInfoTem( $rowManager );
            
            break;
        }
    case 'do_edit': {
            $oModelTem->setData('id',$_POST['id']);
            if($_POST['active_dedault'] == 'on') {
                $oModelTem->setData('active_dedault', 1);
                $set_default = true;
            }else {
                $oModelTem->setData('active_dedault', 0);
            }

            if ($_POST['status'] == 'on') {
                $oModelTem->setData('status', 1);
            }else {
                $oModelTem->setData('status', 0);
            }
            $result = $oModelTem->updateTemplate();
            $oModelTem->setData('explanation', $_POST['explanation']);
            $oModelTem->setData('damage', $_POST['damage']);
            $oModelTem->setData('dagame', $_POST['dagame']);
            $oModelTem->setData('notes', $_POST['notes']);
            $oModelTem->setData('size', $_POST['size']);
            $oModelTem->updateTemplateFiles();
            if($set_default) {
                $arr = $oModelTem->dogetAllRecord();
                foreach($arr as $key=>$value) {
                    if($_POST['id']!=$value['id']) {
                        $oModelTem->setData('active_dedault', 0);
                    }else {
                        $oModelTem->setData('active_dedault', 1);
                    }
                    if($value['status']==1) {
                        $oModelTem->setData('status', 1);
                    }else {
                        $oModelTem->setData('status', 0);
                    }
                    $oModelTem->setData('id', $value['id']);
                    $oModelTem->setData('name', $value['name']);
                    $oModelTem->updateTemplate();
                }
            }
            
            if(!$result) {
                $sErrorMsg =  $oModelTem->aLanguage['ALERT']['form_cannot_submit'] . $sErrorMsg;
                $oModelTem->setErrorMsg($sErrorMsg);
            }else {
                $category_id=$oModelTem->getData('id');
                redir('?sMod=template_index');
            }
            break;
        }
}

$oViewTem->displayPage($oModelTem);
